function layoutRepositoryViewPort(layout, node) {
    mainPanel.activate(contentPanel);
    if (layout == undefined) {
        layout = contentPanel.layout;
    }
    layout.setActiveItem("main-content-repository-panel");
    Ext.Ajax.request({
        url: node.attributes.uri + "/isOwner",
        method: "GET",
        params: {
            "user_uri": LoggedInUser.userUri
        },
        success: function() {
            Ext.getDom("main-content-repository-item").innerHTML = "";
            mayUpdateReposComponents(node);
        },
        failure: function() {
            Ext.getDom("main-content-repository-item").innerHTML = "Read Only Access Allowed";
            Ext.getDom("main-content-repository-addsub").innerHTML = "";
            Ext.getDom("main-content-repository-upload").innerHTML = "";

            eventManager.fireStatusMessageEvent({ text: "Read Only Access Allowed", level: "info" });
        }
    });

}

function mayUpdateReposComponents(node) {
    renderReposSubFolderForm(node);

    var subfolderDiv = Ext.getDom("main-content-repository-addsub");
    Ext.DomHelper.insertHtml("beforeEnd", subfolderDiv, "<br>");

    renderReposUploadFileForm(node);
    var uploadDiv = Ext.getDom("main-content-repository-upload");
    Ext.DomHelper.insertHtml("beforeEnd", uploadDiv, "<br>");
}


function renderReposSubFolderForm(node) {
    Ext.getDom("main-content-repository-addsub").innerHTML = "";
    var uri = node.attributes.uri;
    var subfolderFormPanel = new Ext.form.FormPanel({
        id: 'reposAddSubFormId',
        title: 'New Folder',
        method: 'POST',
        renderTo: "main-content-repository-addsub",
        border: true,
        items: [
            new Ext.form.FieldSet({
                autoHeight: true,
                autoWidth: true,
                border: false,
                items: [
                    new Ext.form.TextField({
                        fieldLabel: 'New Folder',
                        defaultAutoCreate : {tag:"input", size: "40", autocomplete: "off"},
                        name: 'reposNewSubfolder',
                        id: 'reposNewSubfolderId',
                        allowBlank: false,
                        listeners: {
                            focus: function(tf) {
                                Ext.getCmp("addRepoSubfolderButtonId").enable();
                            }
                        }
                    }),
                    new Ext.Button({
                        id: 'addRepoSubfolderButtonId',
                        disabled: true,
                        text: 'Add',
                        listeners: {
                            click: function() {
                                var subfolder = Ext.getCmp("reposNewSubfolderId").getValue();
                                if (subfolder == undefined || subfolder.length < 1) {
                                    Ext.MessageBox.alert("Folder name is invalid, can not be blank or contain illegal characters");
                                    return false;
                                }
                                var postUrl = uri + "/path/" + subfolder + "/create";
                                Ext.Ajax.request({
                                    url: postUrl,
                                    method: "POST",
                                    success: function() {
                                        eventManager.fireStatusMessageEvent({ text: "Folder " + subfolder + " Added Successfully", level: "info" });
                                        expandNode(node);
                                        eventManager.fireNodeSelectEvent(node);
                                    },
                                    failure: function() {
                                        eventManager.fireStatusMessageEvent({ text: "Failed to add New Folder", level: "error" });
                                    }
                                });
                            }
                        }
                    })
                ]
            })
        ]
    });
    subfolderFormPanel.render();
}


function renderReposUploadFileForm(node)
{

    Ext.getDom("main-content-repository-upload").innerHTML = "";
    var uri = node.attributes.uri;

    var uploadFileFormComponent = new Ext.form.FormPanel({
        id: 'reposUploadFileForm',
        title: 'Upload File',
        renderTo: "main-content-repository-upload",
        method: 'POST',
        fileUpload : true,
        border: true,
        items: [
            new Ext.form.FieldSet({
                autoHeight: true,
                autoWidth: true,
                border: false,
                items: [
                    new Ext.form.TextField({
                        fieldLabel: 'Select file',
                        defaultAutoCreate : {tag:"input", enctype:"multipart/form-data", type:"file", size: "35", autocomplete: "off"},
                        name: 'FILE',
                        id: 'reposUploadFileNameId',
                        allowBlank: false,
                        listeners:{
                            focus: function(tf) {
                                Ext.getCmp("uploadRepoFileButtonId").enable();
                            }
                        }
                    }),
                    new Ext.Button({
                        id: 'uploadRepoFileButtonId',
                        text: 'Upload',
                        disabled: true,
                        listeners: {click:
                                function() {
                                    var formPanel = Ext.getCmp('reposUploadFileForm');
                                    var uploadFileName = Ext.getCmp("reposUploadFileNameId").getValue();
                                    if (uploadFileName == undefined || uploadFileName.length < 1) {
                                        Ext.MessageBox.alert("File must be selected before uploading");
                                        return false;
                                    }
                                    //check to see if file exists before
                                    var uploadFileUrl = uri + "/path/" + uploadFileName;
                                    Ext.Ajax.request({
                                        url: uploadFileUrl + "/meta",
                                        method: "GET",
                                        success: function(response) {
                                            var status = response.status;
                                            Ext.MessageBox.confirm('Message', 'File ' + uploadFileName + ' exists, upload new version?', function(btn) {
                                                if (btn == 'yes') {
                                                    upload(node, formPanel, uri + "/path", uploadFileName);
                                                } else {
                                                    eventManager.fireStatusMessageEvent({ text:'File upload cancelled', level: "info" });
                                                    return true;
                                                }
                                            });
                                        },
                                        failure: function(response) {
                                            var status = response.status;
                                            if (status == '404') {
                                                upload(node, formPanel, uri + "/path", uploadFileName);
                                            }
                                        }
                                    });

                                }
                        }
                    })
                ]
            })
        ]
    });
    uploadFileFormComponent.render();
}
